package com.jsz.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.jsz.constant.MessageConstant;
import com.jsz.constant.RedisConstant;
import com.jsz.entity.PageResult;
import com.jsz.entity.QueryPageBean;
import com.jsz.entity.Result;
import com.jsz.pojo.Setmeal;
import com.jsz.service.SetmealService;
import com.jsz.utils.QiniuUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.UUID;

/**
 * 套餐管理
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Reference
    private SetmealService setmealService;

    @Autowired
    private JedisPool jedisPool;

    //文件上传
    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile) {
        //获取原始图片的文件名
        String originalFilename = imgFile.getOriginalFilename();
        //获取原始文件名,已. 结尾的后缀名的索引位置
        int index = originalFilename.lastIndexOf(".");
        //截取原始文件中后缀名, 以.结尾(包含.在内)的文件后缀名
        String extention = originalFilename.substring(index);
        //拼接新文件的文件名 与动态拼接文件名后缀名
        String fileName = UUID.randomUUID().toString() + extention;
        try {
            //将文件上传到七牛云服务器中, 调用七牛云工具类
            QiniuUtils.upload2Qiniu(imgFile.getBytes(), fileName);

            //将文件储存到redis中, 基于redis中的set集合存储
            //添加数据到 redis中的set集合里，集合的名字setmealPicResources,添加了 fileName（图片的名字 ）    // 表示向 redis 的set 集合中添加数据
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);
            //上传文件成功
            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS, fileName);
        } catch (Exception e) {
            e.printStackTrace();
            //上传文件失败
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
    }

    //添加数据
    @RequestMapping("/add/{checkgroupIds}")
    @PreAuthorize("hasAuthority('SETMEAL_ADD')")//添加套餐信息权限
    public Result add(@PathVariable Integer[] checkgroupIds, @RequestBody Setmeal setmeal) {
        try {
            //调用业务层操作数据
            setmealService.add(checkgroupIds, setmeal);
            //添加数据成功
            return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            //添加数据失败
            return new Result(false, MessageConstant.ADD_SETMEAL_FAIL);
        }
    }

    //分页数据查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        //调试
        System.out.println("SetmealController中的findPage方法调用了 queryPageBean:" + queryPageBean);
        //调用业务层操作数据
        PageResult pageResult = setmealService.findPage(queryPageBean);
        return pageResult;
    }

    //根据ID查询
    @RequestMapping("/findById/{id}")
    @PreAuthorize("hasAuthority('SETMEAL_QUERY')")//查询套餐信息权限
    public Result findById(@PathVariable Integer id) {
        try {
            //调用业务层层操作数据
            Setmeal setmeal = setmealService.findById(id);
            //查询成功
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, setmeal);
        } catch (Exception e) {
            e.printStackTrace();
            //查询失败
            return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }

    //根据ID查询套餐表与中间表
    @PreAuthorize("hasAuthority('SETMEAL_QUERY')")//查询套餐信息权限
    @RequestMapping("/findCheckGroupIdBySetmealId/{id}")
    public Result findCheckGroupIdBySetmealId(@PathVariable Integer id) {
        try {
            List<Integer> checkItemIds = setmealService.findCheckGroupIdBySetmealId(id);
            //查询成功
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkItemIds);
        } catch (Exception e) {
            e.printStackTrace();
            //查询失败
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    //修改数据
    @RequestMapping("/update/{checkgroupIds}")
    @PreAuthorize("hasAuthority('SETMEAL_EDIT')")//修改套餐信息权限
    public Result update(@PathVariable Integer[] checkgroupIds, @RequestBody Setmeal setmeal) {
        try {
            //调用业务层操作数据
            setmealService.update(checkgroupIds, setmeal);
            //修改数据成功
            return new Result(true, MessageConstant.EDIT_SETMEAL_SUCCESS);
        } catch (Exception e) {
            //修改数据失败
            return new Result(false, MessageConstant.EDIT_SETMEAL_FAIL);
        }
    }

    //删除数据
    @RequestMapping("/deleteById/{id}")
    @PreAuthorize("hasAuthority('SETMEAL_DELETE')")//删除套餐信息权限
    public Result deleteById(@PathVariable Integer id) {
        try {
            //调用业务层操作数据
            setmealService.deleteById(id);
            //删除数据成功
            return new Result(true, MessageConstant.DELETE_SETMEAL_SUCCESS);
        } catch (Exception e) {
            //删除数据失败
            return new Result(false, MessageConstant.DELETE_SETMEAL_FAIL);
        }
    }

    //查询所有的套餐信息
    @RequestMapping("/findAll")
    public Result findAll(){
        try{
           List<Setmeal> list= setmealService.findAll();
            //查询成功
            return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            //查询失败
            return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }
}
